package com.xhblogs.book.controller;


import com.xhblogs.book.pojo.Admin;
import com.xhblogs.book.pojo.User;
import com.xhblogs.book.service.LoginService;
import com.xhblogs.book.utils.TreePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/api/v1/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private TreePojo treePojo;


    @PostMapping("/jugLogin")
    public Object isLogin(
//                            @RequestParam("permission") int permission
//                            ,@RequestParam("username") String username
//                            ,@RequestParam("password") String pwd
                            @RequestBody Map map1
                            ) {
        HashMap<String, Object> map = new HashMap<>();
//        System.out.println("AAAAAAA:" + Integer.parseInt(map1.get("permission").toString()));
//        System.out.println(String.valueOf(map1.get("username")) + "" + String.valueOf(map1.get("password")));
        if (Integer.parseInt(map1.get("permission").toString()) == 0) {
            Admin admin = loginService.isAdminLogin(String.valueOf(map1.get("username")), String.valueOf(map1.get("password")));
            if (admin == null) {
                map.put("isOk", false);
            }else {
                map.put("isOk", true);
                map.put("info", admin);
            }

        }else {
            User user = loginService.isUserLogin(String.valueOf(map1.get("username")), String.valueOf(map1.get("password")));
            if (user == null) {
                map.put("isOk", false);
            }else {
                map.put("isOk", true);
                map.put("info", user);
            }
        }

        return map;
     }

}
